From: Krinkle Date: Mon, 5 Sep 2011 21:51:04 +0000 (+0000) Subject: mediawiki.html: mediawiki.html: Add support for numbers and booleans X-Git-Tag: 1.31.0-rc.0~27887 X-Git-Url: http://git.cyclocoop.org/%7D%7Cconcat%7B?a=commitdiff_plain;h=1588968960c4c763ff3234e5b474db2aa965bfaa;p=lhc%2Fweb%2Fwiklou.git mediawiki.html: mediawiki.html: Add support for numbers and booleans * Tests introduced in r96305 work now * (bug 30774) - mediawiki.html: Add support for numbers and booleans -- * Removed unneeded value-attribute in one the tests * Changed if-else intro a switch. (to avoid calling the typeof operator multiple times and making the code a bit more readable) --- diff --git a/resources/mediawiki/mediawiki.js b/resources/mediawiki/mediawiki.js index cf89e03511..23d82141c1 100644 --- a/resources/mediawiki/mediawiki.js +++ b/resources/mediawiki/mediawiki.js @@ -1237,9 +1237,17 @@ window.mw = window.mediaWiki = new ( function( $ ) { * Returns
*/ this.element = function( name, attrs, contents ) { - var s = '<' + name; + var v, s = '<' + name; for ( var attrName in attrs ) { - s += ' ' + attrName + '="' + this.escape( attrs[attrName] ) + '"'; + v = attrs[attrName]; + // Convert name=true, to name=name + if ( v === true ) { + v = attrName; + // Skip name=false + } else if ( v === false ) { + continue; + } + s += ' ' + attrName + '="' + this.escape( v ) + '"'; } if ( contents === undefined || contents === null ) { // Self close tag @@ -1248,20 +1256,29 @@ window.mw = window.mediaWiki = new ( function( $ ) { } // Regular open tag s += '>'; - if ( typeof contents === 'string' ) { - // Escaped - s += this.escape( contents ); - } else if ( contents instanceof this.Raw ) { - // Raw HTML inclusion - s += contents.value; - } else if ( contents instanceof this.Cdata ) { - // CDATA - if ( /<\/[a-zA-z]/.test( contents.value ) ) { - throw new Error( 'mw.html.element: Illegal end tag found in CDATA' ); - } - s += contents.value; - } else { - throw new Error( 'mw.html.element: Invalid type of contents' ); + switch ( typeof contents ) { + case 'string': + // Escaped + s += this.escape( contents ); + break; + case 'number': + case 'boolean': + // Convert to string + s += '' + contents; + break; + default: + if ( contents instanceof this.Raw ) { + // Raw HTML inclusion + s += contents.value; + } else if ( contents instanceof this.Cdata ) { + // CDATA + if ( /<\/[a-zA-z]/.test( contents.value ) ) { + throw new Error( 'mw.html.element: Illegal end tag found in CDATA' ); + } + s += contents.value; + } else { + throw new Error( 'mw.html.element: Invalid type of contents' ); + } } s += ''; return s; diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.test.js index 1220ce12f1..bae323f3bd 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.test.js @@ -200,11 +200,10 @@ test( 'mw.html', function() { equal( mw.html.element( 'option', { - value: 'foo', selected: true }, 'Foo' ), - '', + '', 'Attributes may have boolean values. True copies the attribute name to the value.' );